//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.highglass.videoplayer.subTitle;

import android.graphics.Color;

import com.highglass.videoplayer.SubContentUtil;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class AssDecoder extends SubTitleDecoder {
    private static Map<Integer, String> assLanMap = new TreeMap();

    static {
        assLanMap.put(Integer.valueOf(0), "ANSI");
        assLanMap.put(Integer.valueOf(1), "DEFAULT");
        assLanMap.put(Integer.valueOf(2), "SYMBOL");
        assLanMap.put(Integer.valueOf(128), "SHIFTJIS");
        assLanMap.put(Integer.valueOf(129), "HANGEUL");
        assLanMap.put(Integer.valueOf(134), "GB2312");
        assLanMap.put(Integer.valueOf(136), "CHINESEBIG5");
        assLanMap.put(Integer.valueOf(255), "OEM");
        assLanMap.put(Integer.valueOf(130), "JOHAB");
        assLanMap.put(Integer.valueOf(177), "HEBREW");
        assLanMap.put(Integer.valueOf(178), "ARABIC");
        assLanMap.put(Integer.valueOf(161), "GREEK");
        assLanMap.put(Integer.valueOf(162), "TURKISH");
        assLanMap.put(Integer.valueOf(163), "VIETNAMESE");
        assLanMap.put(Integer.valueOf(222), "THAI");
        assLanMap.put(Integer.valueOf(238), "EASTEUROPE");
        assLanMap.put(Integer.valueOf(204), "RUSSIAN");
        assLanMap.put(Integer.valueOf(77), "MAC");
        assLanMap.put(Integer.valueOf(186), "BALTIC");
    }

    public AssDecoder() {
    }

    private static int getAssTime(String str) {
        boolean targetTime = false;
        String[] strArray = str.split("\\.");
        String[] dotArray = strArray[0].split(":");
        String strHour = dotArray[0];
        String strMinute = dotArray[1];
        String strSecond = dotArray[2];
        String strMicosecond = strArray[1];
        int hour = Integer.parseInt(strHour);
        int minute = Integer.parseInt(strMinute);
        int second = Integer.parseInt(strSecond);
        int micosecond = Integer.parseInt(strMicosecond);
        int targetTime1 = hour * 3600000 + minute * '\uea60' + second * 1000 + micosecond;
        return targetTime1;
    }

    public Map<String, List<SubTitleContent>> decodeSubTitle(String subTitlePath, String encode) throws Exception {
        BufferedReader br = SubContentUtil.getEncodeType(subTitlePath);
        String subTitleLine = null;
        String langStr = null;
        String styleName = null;
        String markedStr = null;
        String lanClass = null;
        String lanClassStr = null;
        Integer langVal = Integer.valueOf(0);
        int splitCount = 0;
        int fontSizeIndex = -1;
        int fontColorIndex = -1;
        TreeMap subTitleStypeMap = new TreeMap();
        TreeMap subTitleMap = new TreeMap();
        TreeMap subTitleStyleLanMap = new TreeMap();
        TreeMap subTimeMap = new TreeMap();
        int count = 0;
        StringBuffer subTitleSb = null;

        while((subTitleLine = br.readLine()) != null && !SubContentUtil.stopDecodeFlag) {
            subTitleLine = subTitleLine.trim();
            String[] e;
            int var31;
            if(subTitleLine.toUpperCase().startsWith("STYLE:")) {
                langStr = subTitleLine.substring(subTitleLine.lastIndexOf(",") + 1, subTitleLine.length());
                langVal = Integer.valueOf(langStr);
                styleName = subTitleLine.split(":")[1].trim().split(",")[0].trim();
                assLanMap.containsKey(langVal);
                subTitleStyleLanMap.put(styleName, langVal);
                if(fontSizeIndex != -1 && fontColorIndex != -1) {
                    e = subTitleLine.substring("STYLE:".length()).split(",");
                    boolean tagType = false;
                    boolean markedSb = false;
                    String content = null;

                    try {
                        var31 = Integer.valueOf(e[fontSizeIndex]).intValue();
                        content = e[fontColorIndex].toUpperCase().replace("&H", "#");
                        int var29 = Color.parseColor(content);
                        subTitleStypeMap.put(styleName, new SubTitleStyle(var31, var29));
                    } catch (Exception var27) {
                        var27.printStackTrace();
                    }
                }
            }

            if(subTitleLine.toUpperCase().startsWith("FORMAT:")) {
                e = subTitleLine.substring("FORMAT:".length()).split(",");
                splitCount = e.length;

                for(var31 = 0; var31 < splitCount; ++var31) {
                    if("FONTSIZE".equals(e[var31].trim().toUpperCase())) {
                        fontSizeIndex = var31;
                    } else if("PRIMARYCOLOUR".equals(e[var31].trim().toUpperCase())) {
                        fontColorIndex = var31;
                    }
                }
            }

            if(subTitleLine.toUpperCase().startsWith("DIALOGUE:")) {
                markedStr = subTitleLine.substring(subTitleLine.indexOf(":")).replaceAll("Marked=", "").replaceAll("\\\\(n|N)", "<br>").trim();
                AssTokenizer var28 = new AssTokenizer(new InputStreamReader(new ByteArrayInputStream(markedStr.getBytes(SubContentUtil.encodeType)), SubContentUtil.encodeType));
                var31 = AssTokenizer.ASS_UNKNOWN;
                StringBuffer var30 = new StringBuffer();

                while((var31 = var28.nextAssText()) != AssTokenizer.ASS_EOF) {
                    if(var31 == AssTokenizer.ASS_TEXT) {
                        var30.append(var28.sval);
                    }
                }

                markedStr = var30.toString();
                SubTitleContent var32 = new SubTitleContent();
                var32.setmSubTitleIndex(count++);
                String[] markedArray = markedStr.split(",");
                if(markedArray != null && markedArray.length != 0) {
                    lanClassStr = markedArray[3].trim();
                    if(lanClassStr.startsWith("*")) {
                        lanClassStr = lanClassStr.replace("*", "");
                    }

                    lanClass = String.valueOf(subTitleStyleLanMap.get(lanClassStr));
                    subTitleSb = new StringBuffer();

                    for(int k = splitCount - 1; k < markedArray.length; ++k) {
                        subTitleSb.append(markedArray[k]);
                    }

                    if(subTitleStypeMap != null && subTitleStypeMap.containsKey(lanClassStr)) {
                        var32.setmSubTitleLine("<font size=\"" + ((SubTitleStyle)subTitleStypeMap.get(lanClassStr)).getFontSize() + "\" color=\"" + ((SubTitleStyle)subTitleStypeMap.get(lanClassStr)).getFontColor() + "\">" + subTitleSb.toString() + "</font>");
                    } else {
                        var32.setmSubTitleLine(subTitleSb.toString());
                    }

                    var32.setmSubTitleStartTime(getAssTime(markedArray[1].trim()));
                    var32.setmSubTitleEndTime(getAssTime(markedArray[2].trim()));
                    var32.setmLanguageClass(lanClassStr);
                    if(subTitleMap.get(lanClassStr) == null) {
                        subTitleMap.put(lanClassStr, new ArrayList());
                    }

                    if(!subTimeMap.containsKey(Integer.valueOf(var32.getmSubTitleStartTime()))) {
                        subTimeMap.put(Integer.valueOf(var32.getmSubTitleStartTime()), var32);
                    } else if(var32.getmLanguageClass().equals(((SubTitleContent)subTimeMap.get(Integer.valueOf(var32.getmSubTitleStartTime()))).getmLanguageClass())) {
                        var32 = (SubTitleContent)subTimeMap.get(Integer.valueOf(var32.getmSubTitleStartTime()));
                        if(subTitleStypeMap != null && subTitleStypeMap.containsKey(lanClassStr)) {
                            var32.setmSubTitleLine("<font size=\"" + ((SubTitleStyle)subTitleStypeMap.get(lanClassStr)).getFontSize() + "\" color=\"" + ((SubTitleStyle)subTitleStypeMap.get(lanClassStr)).getFontColor() + "\">" + var32.getmSubTitleLine() + "<br>" + subTitleSb.toString() + "</font>");
                        } else {
                            var32.setmSubTitleLine(var32.getmSubTitleLine() + "<br>" + subTitleSb.toString());
                        }
                    }

                    if(!((List)subTitleMap.get(lanClassStr)).contains(var32)) {
                        ((List)subTitleMap.get(lanClassStr)).add(var32);
                    }
                }
            }
        }

        try {
            br.close();
        } catch (Exception var26) {
            var26.printStackTrace();
        }

        SubContentUtil.SortAllList(subTitleMap);
        return subTitleMap;
    }
}
